package com.example.pxxy_news.activity;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pxxy_news.R;
import com.example.pxxy_news.adapter.CommentAdapter;
import com.example.pxxy_news.application.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import entity.Comment;

public class ShowNewsActivity extends BaseActivity implements OnClickListener{
	private TextView tv_title,tv_content,tv_author,tv_time,tv_id; 
	private EditText et_comment;
	private ImageView image_comment;
	private int mNewsId;
	private ListView mListView;
	public static String URL_COMMENT = "http://10.102.41.48:8080/news_server/comment";
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				List<Comment> list = (List<Comment>) msg.obj;
				if(list.size()>0){
					CommentAdapter ca = new CommentAdapter(list);
					mListView.setAdapter(ca);
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shownews_2);
		initView();
		Intent in = getIntent();
		Bundle bundle = in.getExtras();
		mNewsId = bundle.getInt("newsId");
		tv_title.setText(bundle.getString("title"));
		tv_content.setText(bundle.getString("content"));
		tv_author.setText(bundle.getString("author"));
		tv_time.setText(bundle.getString("time"));
		tv_id.setText(bundle.getInt("newsId")+"");
		setCommentAdapter();
	}
	
	private void setCommentAdapter() {
		OkHttpClient client = new OkHttpClient();
		RequestBody body = new FormBody.Builder().add("operation", "findCommentById")
				.add("newsId", mNewsId+"")
				.build();
		Request request = new Request.Builder()
				.url(URL_COMMENT)
				.post(body).build();
		client.newCall(request).enqueue(new Callback() {
			@Override
			public void onResponse(Call arg0, Response arg1) throws IOException {
				String commentJson = arg1.body().string();
				Log.d("news", commentJson);
				Gson gson = new Gson();
				List<Comment> list = gson.fromJson(commentJson,
						new TypeToken<List<Comment>>() {
						}.getType());
				Log.d("news", list.size()+"");
				Message msg = Message.obtain();
				msg.what = 1;
				msg.obj = list;
				handler.sendMessage(msg);
			}
			
			@Override
			public void onFailure(Call arg0, IOException arg1) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	private void initView() {
		tv_title = (TextView) findViewById(R.id.a_tv_title);
		tv_content = (TextView) findViewById(R.id.a_tv_content);
		tv_author = (TextView) findViewById(R.id.a_tv_author);
		tv_time = (TextView) findViewById(R.id.a_tv_time);	
		tv_id = (TextView) findViewById(R.id.a_tv_id);
		et_comment = (EditText) findViewById(R.id.a_et_comment);
		image_comment = (ImageView) findViewById(R.id.a_image_comment);
		mListView = (ListView) findViewById(R.id.listview_comment);
		image_comment.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.a_image_comment:
			insertComment();
			break;

		default:
			break;
		}
		
	}
	private void insertComment() {
		//��������
		String commentStr = et_comment.getText().toString();
		//�������ݲ���С��5
		if(commentStr.length()<5){
			Toast.makeText(this, "������5�������ϵ�����", 0).show();
			return;
		}
		SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
		//�����û�id
		int userId = sp.getInt("_id",0);
		//�����û���
		String userName = sp.getString("username", "");
		//��������id
		int articleId = mNewsId;
		//����ʱ��
		Date date = new Date();
		DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String punchTime = simpleDateFormat.format(date);
		
		//�򵥵�������Ч���ж�
		if(userId!=0 && !userName.equals("") &&articleId>0){
			//����Servlet
			OkHttpClient client = new OkHttpClient();
			RequestBody body = new FormBody.Builder().add("operation", "insert")
					.add("userid", userId+"")
					.add("articleid",articleId+"")
					.add("userName",userName)
					.add("comment", commentStr)
					.add("createTime", punchTime)
					.build();
			Request request = new Request.Builder()
					.url(URL_COMMENT)
					.post(body).build();
			
			client.newCall(request).enqueue(new Callback() {
				
				@Override
				public void onResponse(Call arg0, Response arg1) throws IOException {
					// TODO Auto-generated method stub
					String result = arg1.body().string();
					Log.d("news", result);
					if(result.equals("success")){
						Looper.prepare();
						Toast.makeText(MyApplication.getContext(), "���۳ɹ�", 0).show();
						Looper.loop();
					}else{
						Looper.prepare();
						Toast.makeText(MyApplication.getContext(), "����ʧ��", 0).show();
						Looper.loop();
					}
				}
				
				@Override
				public void onFailure(Call arg0, IOException arg1) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		
	}
}
